package cn.tedu.straw.portal.controller;


import cn.tedu.straw.commons.dto.CommentDTO;
import cn.tedu.straw.commons.model.Comment;
import cn.tedu.straw.commons.security.UserInfo;
import cn.tedu.straw.portal.service.ICommentService;
import cn.tedu.straw.commons.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-07-29
 */
@RestController
@RequestMapping("/v1/comments")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @RequestMapping("/post")
    public R<Comment> post(CommentDTO commentDTO, @AuthenticationPrincipal UserInfo userInfo){
        Comment comment = commentService.post(commentDTO, userInfo.getId(), userInfo.getNickname());
        return R.ok(comment);
    }

    @RequestMapping("/{id}/delete")
    public R<Comment> delete(@PathVariable("id") Integer id, @AuthenticationPrincipal UserInfo userInfo){
        Comment delete = commentService.delete(id, userInfo.getId(), userInfo.getType());
        return R.ok(delete);
    }

    @RequestMapping("/{commentId}/update")
    public R<Comment> update(@PathVariable("commentId") Integer commentId,String content, @AuthenticationPrincipal UserInfo userInfo){
        Comment update = commentService.update(commentId, content, userInfo.getId(), userInfo.getType());
        return R.ok(update);
    }

}
